/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.lizg.Controller;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LizG
 */
public class MessReceive {

    private static String ip_send;
    private DatagramSocket ds;
    private String mess;

    public MessReceive() {
    }

    public String getMess() {
        return mess;
    }

    public void setIp(String ip) {
        MessReceive.ip_send = ip;
    }

    public void receiveMess() {
        try {
            // 创建接收端的Socket对象
            ds = new DatagramSocket(8189);
            // 创建一个包裹
            byte[] bys = new byte[64];
            DatagramPacket dp = new DatagramPacket(bys, bys.length);

            // 接收数据
            ds.receive(dp);

            // 解析数据
            this.mess = new String(dp.getData(), 0, dp.getLength());

            System.out.println("send发来的：" + mess);//test
//            if (mess.equals("@send")) {
//                String mess = "@receive";
//                dp = new DatagramPacket(mess.getBytes(), mess.getBytes().length,
//                        InetAddress.getByName(ip_send), 8089);
//                // 发送数据
//                ds.send(dp);
//            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("mess接收错误");
        } finally {
            ds.close();
        }
        // 释放资源
        // 接收端应该一直开着等待接收数据，是不需要关闭

    }
}
